﻿using System;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;

namespace DecodingSharePoint.WebControls
{
    // TextField BaseFieldControl
    public class DS_Regex_FieldControl : BaseFieldControl
    {
        protected Label DS_Regex_ValueForDisplay;

        protected override string DefaultTemplateName
        {
            get
            {
                if (this.ControlMode == SPControlMode.Display)
                {
                    return this.DisplayTemplateName;
                }
                else
                {
                    return "DS_Regex_FieldControl";
                }
            }
        }

        public override string DisplayTemplateName
        {
            get
            {
                return "DS_Regex_FieldControl_ForDisplay";
            }
            set
            {
                base.DisplayTemplateName = value;
            }
        }

        protected TextBox txtRegexField;
        protected override void CreateChildControls()
        {
            if (this.Field != null)
            {
                base.CreateChildControls();
                this.txtRegexField = (TextBox)TemplateContainer.FindControl("txtRegexField");
                this.DS_Regex_ValueForDisplay = (Label)TemplateContainer.FindControl("DS_Regex_ValueForDisplay");
            }
            if (this.ControlMode != SPControlMode.Display)
            {
                if (!this.Page.IsPostBack)
                {
                    if (this.ControlMode == SPControlMode.New)
                    {
                        txtRegexField.Text = "";
                    }
                    else
                    {
                        txtRegexField.Text = (String)this.ItemFieldValue;
                    }
                }
            }
            else
            {
                DS_Regex_ValueForDisplay.Text = (String)this.ItemFieldValue;
            }
        }

        public override object Value
        {
            get
            {
                EnsureChildControls();
                return txtRegexField.Text;
            }
            set
            {
                EnsureChildControls();
                base.Value = (String)value;
            }
        }
    }
}
